import mongoose from '../config/DBHelpler'
import moment from 'moment'

const Schema = mongoose.Schema

const CommentsSchema = new mongoose.Schema({
  uid: { type: String },
  tid: { type: String, ref: 'list' },
  cuid: { type: String, ref: 'users' },
  content: { type: String },
  created: { type: Date },
  hands: { type: Number, default: 0 },
  status: { type: String, default: '1' },
  isRead: { type: String, default: '0' },
  isBest: { type: String, default: '0' },
  isShow: { type: String, default: '0' },
})
// CommentsSchema.pre('save', function (next) {
//   this.created = moment().format('YYYY-MM-DD HH:mm:ss')
//   next()
// })
CommentsSchema.pre('update', function (next) {
  this.update = moment().format('YYYY-MM-DD HH:mm:ss')
  next()
})
CommentsSchema.post('save', function (err, doc, next) {
  if (err.name === 'MongoError' && err.code === 11000) {
    next(new Error('Error: Monngose has a duplicate key.'))
  } else {
    next(err)
  }
})

CommentsSchema.statics = {
  // findByID: function (id) {
  //   return this.find({ _id: id })
  // },
  findByTid: function (id, cuid) {
    return this.find({ _id: id, cuid: cuid })
  },
  getCommentsList: function (id, page, limit) {
    return this.find({ tid: id })
      .populate({
        path: 'cuid',
        select: '_id name pic isVip',
        match: { status: { $eq: '0' } },
      })
      .populate({
        path: 'tid',
        select: '_id title status',
      })
      .skip(page * limit)
      .limit(limit)
  },
  queryCount: function (id) {
    return this.find({ tid: id }).countDocuments()
  },
  getMsgList: function (id, page, limit) {
    return this.find({
      uid: id,
      cuid: { $ne: id },
      isRead: { $eq: '0' },
      status: { $eq: '1' },
    })
      .populate({
        path: 'uid',
        select: '_id title',
      })
      .populate({
        path: 'uid',
        select: '_id name',
      })
      .populate({
        path: 'cuid',
        select: '_id name',
      })
      .skip(page * limit)
      .limit(limit)
      .sort({ created: -1 })
  },
  getTotal: function (id) {
    return this.find({ uid: id, isRead: '0', status: '1' }).countDocuments()
  },
  getAllComm: function () {
    return this.find({})
      .populate({
        path: 'cuid',
        select: '_id name status',
      })
      .populate({
        path: 'tid',
        select: 'status title',
      })
  },
  deleteall: function (arr) {
    return this.remove({ _id: { $in: arr } })
  },
  findCommList: function (options) {
    return this.find(options)
      .populate({
        path: 'cuid',
        select: '_id name pic isVip status',
      })
      .populate({
        path: 'uid',
        select: '_id name',
      })
      .populate({
        path: 'tid',
        select: '_id name status',
      })
  },
  findSerch: function (options) {
    return this.find(options)
      .populate({
        path: 'cuid',
        select: '_id name status',
      })
      .populate({
        path: 'uid',
        select: '_id name',
      })
      .populate({
        path: 'tid',
        select: 'status title',
      })
  },
  finddate: function (options) {
    return this.find({
      $and: [
        { created: { $gt: options.created[0] } },
        { created: { $lt: options.created[1] } },
      ],
    })
      .populate({
        path: 'cuid',
        select: '_id name status',
      })
      .populate({
        path: 'uid',
        select: '_id name',
      })
      .populate({
        path: 'tid',
        select: 'status title',
      })
  },
  findStatus: function (id) {
    return this.find({ cuid: id })
      .populate({
        path: 'cuid',
        select: '_id name status',
      })
      .populate({
        path: 'uid',
        select: '_id name',
      })
      .populate({
        path: 'tid',
        select: 'status title',
      })
  },
  findShow: function (status) {
    return this.find({
      status: { $eq: status },
    })
      .populate({
        path: 'cuid',
        select: '_id name status',
      })
      .populate({
        path: 'uid',
        select: '_id name',
      })
      .populate({
        path: 'tid',
        select: 'status title',
      })
  },
}
const CommentsModel = mongoose.model('comments', CommentsSchema)

export default CommentsModel
